﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SMARTMobile.WinPhone.Models;
using System.Collections.ObjectModel;

namespace SMARTMobile.Models.UI
{
    public class TreeModel : BaseModel<TreeModel>
    {
        private Category _selectedCategory;

        public TreeModel()
        {
            Categories = new ObservableCollection<Category>();
        }
        public ObservableCollection<Category> Categories { get; set; }

        public Category SelectedCategory
        {
            get { return _selectedCategory; }
            set
            {

                if (_selectedCategory != null)
                {
                    _selectedCategory.IsCategorySelected = false;
                }
                _selectedCategory = value;
                if (_selectedCategory != null)
                {
                    _selectedCategory.IsCategorySelected = true;
                }
                RaisePropertyChanged(x=>x.SelectedCategory);
            }
        }

        private Category FindCategoryByName(string name, IEnumerable<Category> categories)
        {
            foreach (var category in categories)
            {
                if (category.Name == name)
                {
                    return category;
                }
                var result = FindCategoryByName(name, category.SubCategories);
                if (result != null)
                {
                    return result;
                }
            }
            return null;
        }

        public Category FindCategoryByName(string name)
        {
            return FindCategoryByName(name, Categories);
        }


    }
}
